package top.hmtools.cssManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * css管理者缺省实现
 * @author HyboJ
 * 创建日期：2017-9-27下午4:51:16
 */
public class CssManagerDefault implements ICssManager {
	protected final Logger logger = LoggerFactory.getLogger(CssManagerDefault.class);

	/**
	 * css文件缓存库
	 */
	private static Map<String, String> CSS_REPERTORY = new HashMap<String, String>();

	private List<File> filePaths;

	private String encoding;

	@Override
	public void init(List<File> filePaths, String encoding) {
		// 初始化文件路径集合与文件编码格式，用于刷新
		if (filePaths != null && filePaths.size() > 0) {
			this.filePaths = filePaths;
		}
		if (encoding != null && !"".equals(encoding)) {
			this.encoding = encoding;
		}

		// 将指定文件夹中的所有css文件内容读取到缓存
		String[] extensions = { "css" };
		for (File pathTmp : filePaths) {
			try {
				if (!pathTmp.isDirectory() || !pathTmp.exists()) {
					continue;
				}
				Collection<File> cssFilesTmp = FileUtils.listFiles(pathTmp,extensions, true);
				for (File fileTmp : cssFilesTmp) {
					String fileName = fileTmp.getName();
					String fileContent = FileUtils.readFileToString(fileTmp,encoding);
					CSS_REPERTORY.put(fileName, fileContent);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.getMessage());
				logger.error("获取"+pathTmp.getAbsolutePath()+"目录下的css文件失败");
			}
		}
	}

	@Override
	public void destory() {
		if(CSS_REPERTORY != null){
			CSS_REPERTORY.clear();
		}
	}

	@Override
	public boolean refresh() {
		boolean result = false;
		try {
			this.destory();
			this.init(filePaths, encoding);
			result=true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return result;
	}

	@Override
	public String getCss(String cssNames) {
		if(cssNames != null && !"".equals(cssNames)){
			String[] cssNamesArr = cssNames.split(",");
			return this.getCss(cssNamesArr);
		}else{
			return "\n\r";
		}
	}

	@Override
	public String getCss(List<String> cssNames) {
		if(cssNames != null && cssNames.size()>0){
			String[] cssNamesArr = cssNames.toArray(new String[0]);
			return this.getCss(cssNamesArr);
		}else{
			return "\n\r";
		}
	}

	@Override
	public String getCss(String[] cssNames) {
		StringBuffer sb_result = new StringBuffer("\n\r");
		if(cssNames != null && cssNames.length>0){
			for(String fileName:cssNames){
				String css_content_tmp = CSS_REPERTORY.get(fileName.trim().toLowerCase());
				if(css_content_tmp == null || "".equals(css_content_tmp)){
					continue;
				}
				sb_result.append(css_content_tmp+"\n\r");
			}
			return sb_result.toString();
		}else{
			return "\n\r";
		}
	}

	@Override
	public List<String> listCssFilenames() {
		List<String> result = new ArrayList<String>();
		if(CSS_REPERTORY != null){
			Set<String> keySet = CSS_REPERTORY.keySet();
			result.addAll(keySet);
			Collections.sort(result);
		}
		return result;
	}

}
